package com.multysite.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.multysite.entity.Category;
import com.multysite.entity.News;
import com.multysite.entity.Tag;
import com.multysite.model.CategoryModel;
import com.multysite.model.NewsModel;
import com.multysite.model.TagModel;
import com.multysite.util.StringHelper;

@SuppressWarnings("serial")
public class PostNewsServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			resp.setContentType("text/html");
			resp.setCharacterEncoding("utf-8");

			News obj = new News();
			String title = req.getParameter("post[title]");
			String category = req.getParameter("post[tag]");
			String content = req.getParameter("post[content]");

			// title + alias
			obj.setTitle(title);
			obj.setAlias(StringHelper.replaceVietnamese(title));

			// category + category Alias
			Document doc = Jsoup.parse(content);
			if (category.equals("")) {
				obj.setCategory("Uncategory");
				obj.setCategoryAlias("uncategory");
			} else {
				category = category.replaceAll(",[\\w\\W]$", "");
				String cateAlias = StringHelper.replaceVietnamese(category);
				Category cate = CategoryModel.getById(cateAlias);
				if (cate == null) {
					cate = new Category();
					cate.setTitle(category);
					cate.setAlias(cateAlias);
					cate.setLevel(1);
					cate.setParentAlias("");
					cate.setDescription("");
					CategoryModel.insert(cate);
				}
				obj.setCategory(cate.getTitle());
				obj.setCategoryAlias(cate.getAlias());
			}

			// source
			String source = "";
			try {
				source = doc.select("a").last().attr("href");
			} catch (Exception e) {

			}
			obj.setSource(source);

			// thumb image
			Elements elements = doc.select("img");
			String thumb = "/images/thumb.png";
			String ct = doc.select("body").html().trim();
			if (elements.size() > 0) {
				for (int i = 0; i < elements.size(); i++) {
					ct = ct.replace(elements.get(i).toString(),
							"<div class='image'>" + elements.get(i).toString()
									+ "</div>");
				}
				thumb = elements.get(0).attr("src");
			}
			obj.setThumb(thumb);

			// tag
			String strTag = StringHelper.keyword(doc.text());
			try {
				ArrayList<String> listTag = new ArrayList<String>();
				String[] splitTag = strTag.split(",");
				for (String string : splitTag) {
					String tagAlias = StringHelper.replaceVietnamese(string
							.trim());
					Tag tag = TagModel.getById(tagAlias);
					if (tag != null) {
						List<News> listNews = tag.recoverListNews();
						for (int i = 0; i < listNews.size(); i++) {
							if (listNews.get(i).getAlias()
									.equals(obj.getAlias())) {
								listNews.remove(i);
							}
						}
						listNews.add(obj);
						tag.tranformListNews(listNews);
						TagModel.insert(tag);
					} else {
						tag = new Tag();
						tag.setAlias(tagAlias);
						tag.setTitle(string);
						List<News> listNews = new ArrayList<News>();
						listNews.add(obj);
						tag.tranformListNews(listNews);
						TagModel.insert(tag);
					}
					String objTag = tagAlias + "," + string;
					listTag.add(objTag);
				}
				obj.setTags(listTag);
			} catch (Exception e) {

			}

			// content
			obj.setContent(StringHelper.cleanContent(ct));
			NewsModel.insert(obj);
			resp.getWriter().println("1");
		} catch (Exception e) {
			resp.getWriter().println("Invalid Url");
			e.printStackTrace(System.err);
		}

	}
}
